package com.image.untils;

import java.awt.image.BufferedImage;

public class RGBToGrayUntil {
	
	
	public static BufferedImage transferGrayImage(BufferedImage image) {
		BufferedImage grayImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
		for (int i = 0; i < image.getWidth(); i++) {
			for (int j = 0; j < image.getHeight(); j++) {
				int rgb = image.getRGB(i, j);
				int r = (rgb >> 16) & 0xff;
				int g = (rgb >> 8) & 0xff;
				int b = rgb & 0xff;
				int gray = (r * 299 + g * 587 + b * 114 + 500) / 1000;
				rgb = (255 & 0xff) << 24 | (gray & 0xff) << 16 | (gray & 0xff) << 8 | gray & 0xff;
				grayImage.setRGB(i, j, rgb);
			}
		}
		return grayImage;
	}


}
